package ute.mvc.dao;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ute.mvc.model.Assignment;
import ute.mvc.model.OpenCourse_Subject;
import ute.mvc.model.TestAndQuizz;

@Repository("assignmentDAO")
public class AssignmentDAOImpl implements AssignmentDAO{
	@Autowired
	SessionFactory sessionFactory;

	public void addAssignment(Assignment assignmnent) {
		sessionFactory.getCurrentSession().save(assignmnent);
	}

	public void updateAssignment(Assignment assignmnent) {
		sessionFactory.getCurrentSession().merge(assignmnent);
	}

	public void deleteAssignment(Assignment assignmnent) {
		sessionFactory.getCurrentSession().delete(assignmnent);
	}

	public String getMaxIdInOpenCourseSubject(OpenCourse_Subject subject) {
		Criteria criteria = sessionFactory.getCurrentSession()
				.createCriteria(Assignment.class)
				.add(Restrictions.eq("openCourse_Subject", subject))
				.setProjection(Projections.max("assignmentId"));
		String maxId = (String) criteria.uniqueResult();
		return maxId;
	}

	public Assignment getAssignmentById(String assignmentId) {
		return (Assignment) sessionFactory.getCurrentSession().get(Assignment.class, assignmentId);
	}
}
